package com.yyy.sevice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yyy.entity.TbSpecification;
import com.yyy.entity.TbSpecificationExample;
import com.yyy.mapper.TbSpecificationMapper;
import com.yyy.sevice.TbSpecificationService;
import com.yyy.util.PageResult;
import com.yyy.vo.TbSpecificationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TbSpecificationServiceImpl implements TbSpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Override
    public List<TbSpecification> findAll(){
        return tbSpecificationMapper.selectByExample(null);
    }

    @Override
    public PageResult<TbSpecification> findByPage(Integer page, Integer pageSize){
        //开始分页
        PageHelper.startPage(page,pageSize);
        //查询数据
        List<TbSpecification> tbspecifications = this.findAll();
        //将查询得到的数据转换为Page<Student>对象
        Page<TbSpecification> tbspecificationPage = (Page<TbSpecification>) tbspecifications;
        // 转换为PageResult对象
        return new PageResult<>(tbspecificationPage.getResult(),tbspecificationPage.getTotal());
    }

    @Override
    public PageResult<TbSpecification> search(Integer page, Integer pageSize, TbSpecificationVo tbSpecificationVo){
        // 1. 开始分页
        PageHelper.startPage(page, pageSize);
        // 2. 查询数据
        // 2.1 定义一次查询的实例
        TbSpecificationExample example = new TbSpecificationExample();
        // 2.2 添加查询条件到查询实例中
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        criteria.andSpecNameLike("%" + tbSpecificationVo.getSpecName() + "%");


        List<TbSpecification> tbSpecifications =tbSpecificationMapper.selectByExample(example);
        Page<TbSpecification> tbSpecificationPage = (Page<TbSpecification>) tbSpecifications;
        // 3. 转换为PageResult对象
        return new PageResult<>(tbSpecificationPage.getResult(), tbSpecificationPage.getTotal());
    }

    @Override
    public boolean save(TbSpecification tbSpecification) {
        return tbSpecificationMapper.insert(tbSpecification) > 0;
    }
}
